import React from 'react';
import { Modal, Form } from 'antd';
import { 
  AutoSearch
} from 'components/common';
import {
  queryDataTemplates
} from 'services/chart';

const FormItem = Form.Item;

class Password extends React.Component {
  constructor (props) {
    super(props);
    this.state = {};
    this.onOK = this.onOK.bind(this);
  }

  onOK () {
    const { onOK, form, onCancel, record, getList } = this.props;

    form.validateFields((err, values) => {
      if (!err) {
        onOK({ ...values, eqId: record.id }).then(() => {
          onCancel();
          getList();
        });
      }
    });
  }

  render () {
    const { 
      form, 
      record,
      loading,
      onCancel,
      templateList,
      getTemplateList
     } = this.props;
    const { getFieldDecorator } = form;
    const modalOpts = {
      confirmLoading: loading,
      title: '分配模版',
      width: 500,
      visible: true,
      maskClosable: false,
      okText: '确定',
      cancelText: '取消',
      onOk: this.onOK,
      onCancel
    };
    const formItemLayout = {
      labelCol: {
        span: 6
      },
      wrapperCol: {
        span: 14
      }
    };
    const tempProps = {
      preSearch: true,
      placeholder: '请选择模版列表',
      preFetchList: getTemplateList,
      dataSource: templateList,
      fetchList: queryDataTemplates,
      result: data => data.data.data,
      // onSelect: (params) => { 
      //   // getUserList({ companyId: params.id });
      //  },
      searchKey: 'name',
      keys: ['name']
    };
    console.log(record);
    return (
      <Modal {...modalOpts}>
        <Form>
          <FormItem
            label='模版列表'
            {...formItemLayout}
          >
            {
              getFieldDecorator('dataTemplateId', {
                initialValue: record.dataTemplateId || '',
                rules: [{ required: true, message: '请选择模版列表' }]
              })(
                <AutoSearch {...tempProps} />
              )
            }
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(Password);
